package com.project.service.impl;

import com.project.service.MailService;
import com.project.util.ConvertDateUtils;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Service
@Slf4j
public class MailServiceImpl implements MailService {

    /**
     * 当天的短链接生成次数的key前缀
     */
    private String TODAY_CUR_SIZE = "day_cur_size:";

    /**
     * 当天的短链接解析次数的key前缀
     */
    private String TODAY_USED_SIZE = "day_used_size:";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void sendWeeklyReport() throws IOException {
        // 获取上周的keys列表
        String[] keys = ConvertDateUtils.getWeeklyKeys();
        // 每天的短链接生成次数
        Map<String,Long> genLinkMap = new HashMap<>();
        Map<String,Long> useLinkMap = new HashMap<>();
        for (int i = 0 ;i < 7 ; i++){
            // 查询redis
            String keyGen = TODAY_CUR_SIZE + keys[i];
            String keyUse = TODAY_USED_SIZE + keys[i];
            // 存入Map
            if (redisTemplate.opsForValue().get(keyGen) == null){
                genLinkMap.put(keys[i],0L);
            }else{
                genLinkMap.put(keys[i], (Long) redisTemplate.opsForValue().get(keyGen));
            }
            if (redisTemplate.opsForValue().get(keyUse) == null){
                useLinkMap.put(keys[i],0L);
            }else{
                useLinkMap.put(keys[i], (Long) redisTemplate.opsForValue().get(keyUse));
            }
        }
        // 发送邮件
        this.sendMail(genLinkMap,useLinkMap);

//        System.out.println("Detailed weekly report service execution time：" + mill + "ms");
    }

    /**
     * 将剩余服务次数发送给用户
     * @param remianTotal 剩余次数
     * @param mailAddress 用户邮箱地址
     */
    @Override
    public void sendUserWarning(Integer remianTotal,String mailAddress) {
        long startTime = System.currentTimeMillis();
        //初始化邮件对象
        SimpleMailMessage mail = new SimpleMailMessage();
        //主题
        mail.setSubject("短链接服务次数预警");
        // 文本
        mail.setText("剩余服务次数为：" + remianTotal);
        //发送人
        mail.setFrom("1296948752@qq.com");
        //接受人
        mail.setTo(mailAddress);
        javaMailSender.send(mail);
        long endTime = System.currentTimeMillis();
        long mill = (endTime - startTime) ;
        log.info("Email sending time：" + mill + "ms");
    }

    /**
     * 发送邮件，内容包括上周每天的短链接生成次数、短链接使用次数
     * @param genLinkMap
     * @param useLinkMap
     */
    private void sendMail( Map<String,Long> genLinkMap, Map<String,Long> useLinkMap){
        long startTime = System.currentTimeMillis();
        //初始化邮件对象
        SimpleMailMessage mail = new SimpleMailMessage();
        //主题
        mail.setSubject("短链接服务详情周报");
        //文本
        String context = "短链接生成次数统计："+genLinkMap.toString();
        context = context + "短链接使用次数统计：" + useLinkMap.toString();
        mail.setText(context);
        //发送人
        mail.setFrom("1296948752@qq.com");
        //接受人
        mail.setTo("i_huangshaomei@cvte.com");
        javaMailSender.send(mail);
        long endTime = System.currentTimeMillis();
        long mill = (endTime - startTime) ;
        log.info("Email sending time：" + mill + "ms");

    }

}
